package com.xiaohong.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.order.pojo.Order;
import com.xiaohong.order.pojo.OrderItem;
import com.xiaohong.order.service.OrderItemService;
import com.xiaohong.order.service.OrderService;
import entity.Result;
import entity.StatusCode;
import entity.TokenDecode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Tag(name = "Order接口文档")
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private RedisTemplate redisTemplate;

    /***
     * 分页条件搜索实现Order
     * @param order
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询Order",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Order对象",required = true) Order order, @PathVariable int page, @PathVariable  int size){
        //调用OrderService实现分页条件查询Order
        Page pageInfo = orderService.findPage(order, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现Order
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询Order",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用OrderService实现分页查询Order
        Page pageInfo = orderService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索Order数据
     * @param order
     * @return
     */
    @Operation(summary = "多条件查询Order",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<Order>> findList(@RequestBody(required = false) @Parameter(name = "Order对象",required = true) Order order){
        //调用OrderService实现条件查询Order
        List<Order> list = orderService.findList(order);
        return new Result<List<Order>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除order数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除Order",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用OrderService实现根据ID删除
        orderService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改Order数据
     * @param order
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改Order",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "Order对象",required = true) Order order, @PathVariable String id){
        // 补充id
        order.setId(id);
        //调用OrderService实现修改Order
        orderService.update(order);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Order数据
     * @param order
     * @return
     */
    @Operation(summary = "添加Order",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "Order对象",required = true) Order order){
        String username = (String) TokenDecode.getUserInfo().get("username");
        // 遍历子订单
        List<OrderItem> orderItems = order.getOrderItems();
        int totalNum = orderItems.size(); // 数量合计
        Float totalMoney = 0F; // 金额合计
        Float preMoney = 0F; // 优惠金额
        Float postFee = 0F; // 邮费
        Float payMoney = 0F; // 实付金额
        for (OrderItem orderItem : orderItems) {
            totalMoney += orderItem.getMoney();
            postFee += orderItem.getPostFee();
            payMoney += orderItem.getPayMoney();
        }
        preMoney = totalMoney - payMoney;
        //完善订单信息
        order.setTotalNum(totalNum);
        order.setTotalMoney(totalMoney);
        order.setPreMoney(preMoney);
        order.setPostFee(postFee);
        order.setPayMoney(payMoney);
        order.setBuyerRate(0);
        order.setOrderStatus(0);
        order.setPayStatus(0);
        order.setConsignStatus(0);
        order.setUsername(username);
        order.setUserId(username);

        order.setShippingName("小鸿快递");
        order.setShippingCode("110110");
        //调用OrderService实现添加Order
        orderService.add(order);
        //将子订单存入数据库同时清理redis购物车
        for (OrderItem orderItem : orderItems) {
            orderItem.setOrderId(order.getId());
            // 清空原来的id
            orderItem.setId(null);
            orderItemService.add(orderItem);
            redisTemplate.boundHashOps("Cart_"+username).delete(orderItem.getSkuId());
        }
        return new Result(true,StatusCode.OK,"添加成功",order.getId());
    }

    /***
     * 根据ID查询Order数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询Order",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<Order> findById(@PathVariable String id){
        //调用OrderService实现根据ID查询Order
        Order order = orderService.findById(id);
        return new Result<Order>(true,StatusCode.OK,"查询成功",order);
    }

    /***
     * 查询Order全部数据
     * @return
     */
    @Operation(summary = "查询所有Order",description = "所需权限：")
    @GetMapping
    public Result<List<Order>> findAll(){
        //调用OrderService实现查询所有Order
        List<Order> list = orderService.findAll();
        return new Result<List<Order>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
